//index.js
//获取应用实例
const app = getApp()
import http from '../../../class/Utils/Http.js'
import user from '../../../class/Utils/User.js'
import tips from '../../../class/Utils/Tips.js'

Page({
  data: {
    nolist:0,
    searchname:'',
  },
  
  onLoad: function (options) {
    var that = this

tips.loading()

var term=options.term

that.setData({
  searchname: term
})
console.log(term)

    var user = wx.getStorageSync('user');
    if (user) {
      http.req("/Api/quan_list", { uid: user.userid }, function (res) {
        that.setData({
          myqlist: res.list,
          myqcnt: res.count
        })


        http.req('/syq/search', { terms: term }, function (res) {
          if (res.search_res) {
            var myqcnt = that.data.myqcnt
            var myqlist = that.data.myqlist

            for (var i = 0; i < res.q_info.length; i++) {
              res.q_info[i].changduans = 1;


              //判断会员是否加入该圈
              res.q_info[i].is_join = 0
              if (myqcnt) {
                for (var j = 0; j < myqcnt; j++) {
                  if (myqlist[j].url == res.q_info[i].url) {
                    res.q_info[i].is_join = 1
                  }
                }
              }


            }
            that.setData({
              quan: res.q_info
            })
          } else {
            that.setData({
              nolist: 1
            })
          }
          tips.loaded()
        });




      })
    } else {
      that.setData({
        myqcnt: 0
      })

      http.req('/syq/search', { terms: term }, function (res) {
        if (res.search_res) {
          var mqcnt = that.data.myqcnt
          for (var i = 0; i < res.q_info.length; i++) {
            res.q_info[i].changduans = 1;

          }
          that.setData({
            quan: res.q_info
          })
        } else {
          that.setData({
            nolist: 1
          })
        }
        tips.loaded()
      });


    }




   
  



  },
  onShow: function () {
   
  },
  //搜索跳转页面
  searchlink: function(e) {
   var that = this
   wx.navigateTo({
     url: '/pages/shengyiquan/search/search'
   })
  },
  //监听 page 滚动条位置
  onPageScroll: function (res) {
    var that = this;
    var topscroll = res.scrollTop;
    console.log(res.scrollTop);
    if (topscroll > 50) {
      that.setData({
        searchtig: true,
      })
    } else {
      that.setData({
        searchtig: false,
      })
    }

  },
  //去首页
  gotohome:function(){
    wx.switchTab({
      url: '/pages/index/index'
    })
  },
  //去微商机
  gotoweishangji: function () {
    wx.switchTab({
      url: '/pages/weishangji/list/list'
    })
  },
  //去生意圈
  gotoshengyiquan: function () {
    wx.switchTab({
      url: '/pages/shengyiquan/list/list'
    })
  },
  //去会员中心
  gotomaincenter: function () {
    wx.switchTab({
      url: '/pages/main/menu/menu'
    })
  },
  //长短 介绍切换
  changeintro: function (e) {
    var that = this
    console.log(e)
    var presentindex = e.currentTarget.dataset.index
    var datalist = that.data.quan
    var datalistzhi = datalist[presentindex].changduans

    for (var i = 0; i < datalist.length; i++) {

      if (datalistzhi == 1) {
        datalist[presentindex].changduans = 0

      }
      else {
        datalist[presentindex].changduans = 1
      }
    }


    that.setData({
      quan: datalist
    })
    console.log(datalist)



  },
  //关闭当前页面 去搜索页
  backsearch:function(){
    wx.redirectTo({
      url: '/pages/shengyiquan/search/search'
    })
  },
  jiaru: function (e) {
    var that = this
    user.log_user(that, function (ures) {
      var id = e.currentTarget.dataset.id
      var name = e.currentTarget.dataset.name
      var index = e.currentTarget.dataset.index
      tips.modal("是否加入 '" + name + "'", true, 'function', false, function () {

        var data = {
          uid: ures.userid,
          qid: id
        }
        console.log(data)
        http.req("/Api/add_quan", data, function (res) {
          console.log(res)
          if (res.code == '204') {
            tips.ok(res.msg_val)


            var datalist = that.data.quan
            datalist[index].is_join = 1

            that.setData({
              quan: datalist
            })


          } else {
            tips.error(res.msg_val)
          }
        })
      })

    })
  },
  bindGetUserInfo: function (e) {
    var that = this
    user.user(this, function (res) {
      that.setData({
        shouquan: 0,
        userinfo: res

      })
      if (res.log == 0) {
        that.setData({
          bangdingtoocletag: true
        })
      }
    })

  }

})
